package test3.simple;

import java.util.Arrays;
import java.util.HashMap;

/**
 * 罗马数字转成阿拉伯数字
 */
public class RomanToInt {

    static HashMap<Character, Integer> RomanMap = new HashMap<Character, Integer>() {{
        put('I', 1);
        put('V', 5);
        put('X', 10);
        put('L', 50);
        put('C', 100);
        put('D', 500);
        put('M', 1000);
    }};

    public static int romanToInt(String s) {
        int sum = 0;
        for (int i = 0; i < s.length() - 1; i++) {
            if (RomanMap.get(s.charAt(i)) < RomanMap.get(s.charAt(i+1))) {
                sum -= RomanMap.get(s.charAt(i));
            } else {
                sum += RomanMap.get(s.charAt(i));
            }
        }
        sum += RomanMap.get(s.charAt(s.length() - 1));
        return sum;
    }

    public static void main(String[] args) {
        String str = "IV";
        str = "III";
        str = "IX";
        str = "LVIII";
        str = "MCMXCIV";
        int i = romanToInt(str);
        System.out.println(i);
    }
}
